<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Class_AutoLoader
 *
 * @author luigi.pastore
 * @created  27-apr-2011
 */
// create custom FileNotFoundException exception class

class FileNotFoundException extends Exception
{
    
}

// create custom ClassNotFoundException exception class

class ClassNotFoundException extends Exception
{
    
}

class AutoLoader
{

    public function __construct()
    {
	spl_autoload_register( array( __CLASS__, 'load' ) );
    }

    public static function load( $class )
    {
	// Trucchetto per ambiente di Altervista...
	if( $class == 'finfo' )
	{
	    return;
	}

	$class_name = str_replace( '_', '/', $class );
	
	spl_autoload( $class_name );
//	echo 'Carica: '. $class .'<br>';
//	echo "<pre>";
//	print_r(  get_included_files());
//	echo "</pre>";

	if( class_exists( $class, FALSE ) )
	{
	    return;
	}

	//$class_name{0} = strtolower( $class_name{0} );

	$file = APPLICATION_PATH .'/'.$class_name .'.php';

	if( !file_exists( $file ) )
	{
	    throw new FileNotFoundException( 'File ' . $file . ' not found.' );
	}

	require_once $file;
//
//	unset( $file );

	if( !class_exists( $class, FALSE ) )
	{

	    eval( 'class ' . $class . '{}' );

	    throw new ClassNotFoundException( 'Class ' . $class . ' not found.' );
	}
    }

}

?>
